# 示例路由 - 数据库版本
from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session
from typing import List
from app.schemas.example import ExampleCreate, ExampleUpdate, ExampleResponse
from app.services.example_service import example_service
from app.core.database import get_db

router = APIRouter(
    prefix="/api/examples",
    tags=["examples"],
    responses={404: {"description": "Not found"}},
)

@router.post("/", response_model=ExampleResponse, summary="创建示例")
def create_example(example: ExampleCreate, db: Session = Depends(get_db)):
    """创建新的示例"""
    return example_service.create_example(db=db, example_data=example)

@router.get("/", response_model=List[ExampleResponse], summary="获取所有示例")
def get_examples(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    """获取所有示例列表"""
    return example_service.get_examples(db=db, skip=skip, limit=limit)

@router.get("/count", summary="获取示例总数")
def get_examples_count(db: Session = Depends(get_db)):
    """获取示例总数"""
    count = example_service.count_examples(db=db)
    return {"count": count}

@router.get("/{example_id}", response_model=ExampleResponse, summary="获取示例详情")
def get_example(example_id: int, db: Session = Depends(get_db)):
    """根据ID获取示例详情"""
    example = example_service.get_example_by_id(db=db, example_id=example_id)
    if not example:
        raise HTTPException(status_code=404, detail="示例不存在")
    return example

@router.put("/{example_id}", response_model=ExampleResponse, summary="更新示例")
def update_example(example_id: int, example_update: ExampleUpdate, db: Session = Depends(get_db)):
    """更新示例信息"""
    example = example_service.update_example(
        db=db, 
        example_id=example_id, 
        example_data=example_update
    )
    if not example:
        raise HTTPException(status_code=404, detail="示例不存在")
    return example

@router.delete("/{example_id}", summary="删除示例")
def delete_example(example_id: int, db: Session = Depends(get_db)):
    """删除示例（软删除）"""
    success = example_service.delete_example(db=db, example_id=example_id)
    if not success:
        raise HTTPException(status_code=404, detail="示例不存在")
    return {"message": "示例删除成功"} 